const path = require('path');

const HTMLWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
  entry: './src/index.ts',  // 入口文件
  mode: 'development',
  output: { // 输出配置
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    environment: {  // webpack不使用箭头函数
      arrowFunction: false,
      const: false
    }
  },

  devServer: {
    hot: true,
    port: 3000, // 端口号
  },

  module: { // webpack打包时使用的模块
    rules: [
      {
        test: /\.ts$/,      // 匹配ts后缀文件
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  {
                    targets: { "chrome": "80", "ie": "10" }, // 要兼容的浏览器
                    "corejs": "3",              // 指定corejs版本
                    "useBuiltIns": "usage",     // 按需加载
                  }
                ]
              ]
            }
          },
          'ts-loader',
        ],   // 使用ts-loader
        exclude: /node_modules/, // 排除的文件
      },
      {
        test: /\.less$/,
        use: [
          "style-loader",
          "css-loader",
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  ["postcss-preset-env", { browsers: 'last 2 versions' }]
                ]
              }
            }
          },
          "less-loader"
        ]
      }
    ]
  },

  plugins: [
    new CleanWebpackPlugin(), // 清除之前打包的dist
    new HTMLWebpackPlugin({   // 自定义生成html模板
      title: 'ts',
      template: './src/index.html'
    }),
  ],

  resolve: {
    extensions: ['.ts', '.js']
  }
}